package at.fhooe.stefanirndorfer.event;

import java.util.ArrayList;

public class QueuedDispatchStrategy implements DispatchStrategy {

	private ListenerStrategy m_listenerStrategy;
	private final ArrayList<Event<?>> m_events;
	private final ArrayList<Event<?>> m_eventsForDispatch;

	public QueuedDispatchStrategy() {
		m_events = new ArrayList<Event<?>>();
		m_eventsForDispatch = new ArrayList<Event<?>>();
	}

	@Override
	public void dispatchEvent(Event<?> event) {
		m_eventsForDispatch.add(event);
	}

	@Override
	public void update(double dt) {
		m_events.addAll(m_eventsForDispatch);
		m_eventsForDispatch.clear();

		for (Event<?> event : m_events) {
			m_listenerStrategy.fireEvent(event);
		}
		m_events.clear();
	}

	@Override
	public void setListenerStrategy(ListenerStrategy listenerStrategy) {
		m_listenerStrategy = listenerStrategy;
	}

	@Override
	public void clearEventsForDispatch() {
		m_eventsForDispatch.clear();
	}

}
